import 'package:flutter/material.dart';
import 'dart:math' as math;
import '../../domain/models/device_model.dart';

/// 设备卡片组件
class DeviceCard extends StatelessWidget {
  final DeviceModel device;
  final bool isEditMode;
  final AnimationController animationController;
  final VoidCallback onToggle;

  const DeviceCard({
    Key? key,
    required this.device,
    required this.isEditMode,
    required this.animationController,
    required this.onToggle,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: animationController,
      builder: (context, child) {
        return Transform.rotate(
          angle: isEditMode ? (math.pi / 180) * (10 * animationController.value - 5) : 0,
          child: child,
        );
      },
      child: Container(
        width: MediaQuery.of(context).size.width * 0.45,
        height: 200,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(12),
          boxShadow: [
            BoxShadow(
              color: Colors.grey.withOpacity(0.1),
              spreadRadius: 1,
              blurRadius: 5,
              offset: const Offset(0, 2),
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 设备图片
            Expanded(
              child: Stack(
                children: [
                  ClipRRect(
                    borderRadius: const BorderRadius.only(
                      topLeft: Radius.circular(12),
                      topRight: Radius.circular(12),
                    ),
                    child: Container(
                      width: double.infinity,
                      color: Colors.grey[200],
                      child: Center(
                        child: Icon(
                          _getDeviceIcon(),
                          size: 50,
                          color: Colors.grey[400],
                        ),
                      ),
                    ),
                  ),
                  // 通知图标
                  Positioned(
                    top: 8,
                    right: 8,
                    child: Container(
                      width: 32,
                      height: 32,
                      decoration: BoxDecoration(
                        color: Colors.white,
                        shape: BoxShape.circle,
                      ),
                      child: Icon(
                        Icons.notifications_none,
                        color: Colors.blue[300],
                        size: 20,
                      ),
                    ),
                  ),
                ],
              ),
            ),
            // 设备信息
            Padding(
              padding: const EdgeInsets.all(12.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    device.name,
                    style: const TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  const SizedBox(height: 8),
                  _buildDeviceProperties(),
                  const SizedBox(height: 12),
                  _buildControlButton(),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 获取设备图标
  IconData _getDeviceIcon() {
    switch (device.type) {
      case DeviceType.airConditioner:
        return Icons.ac_unit;
      case DeviceType.airPurifier:
        return Icons.air;
      default:
        return Icons.devices_other;
    }
  }

  /// 构建设备属性显示
  Widget _buildDeviceProperties() {
    if (device.type == DeviceType.airConditioner) {
      return Row(
        children: [
          Text(
            device.properties['mode'] ?? '',
            style: TextStyle(fontSize: 14, color: Colors.grey[600]),
          ),
          const SizedBox(width: 8),
          Text(
            '${device.properties['temperature']}°C',
            style: TextStyle(fontSize: 14, color: Colors.grey[600]),
          ),
          const SizedBox(width: 8),
          Text(
            device.properties['fanSpeed'] ?? '',
            style: TextStyle(fontSize: 14, color: Colors.grey[600]),
          ),
        ],
      );
    } else if (device.type == DeviceType.airPurifier) {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Text(
                '${device.properties['temperature']}°C',
                style: TextStyle(fontSize: 14, color: Colors.grey[600]),
              ),
              const SizedBox(width: 8),
              Text(
                device.properties['humidity'] ?? '',
                style: TextStyle(fontSize: 14, color: Colors.grey[600]),
              ),
            ],
          ),
          const SizedBox(height: 4),
          Text(
            device.properties['pm25'] ?? '',
            style: TextStyle(fontSize: 14, color: Colors.grey[600]),
          ),
        ],
      );
    } else {
      return const SizedBox.shrink();
    }
  }

  /// 构建控制按钮
  Widget _buildControlButton() {
    if (device.type == DeviceType.airConditioner) {
      return GestureDetector(
        onTap: onToggle,
        child: Container(
          width: 40,
          height: 40,
          decoration: BoxDecoration(
            color: device.isOn ? Colors.blue : Colors.grey,
            shape: BoxShape.circle,
          ),
          child: Icon(
            Icons.power_settings_new,
            color: Colors.white,
            size: 20,
          ),
        ),
      );
    } else if (device.type == DeviceType.airPurifier) {
      return Container(
        width: 40,
        height: 40,
        decoration: BoxDecoration(
          color: Colors.white,
          shape: BoxShape.circle,
          border: Border.all(color: Colors.grey[300]!),
        ),
        child: Icon(
          Icons.refresh,
          color: Colors.grey[600],
          size: 20,
        ),
      );
    } else {
      return Switch(
        value: device.isOn,
        onChanged: (_) => onToggle(),
        activeColor: Colors.blue,
      );
    }
  }
}